home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Deutsche Edition 1
/
Deutsche Edition 1.iso
/
amok
/
amok_lha
/
amok15.lha
/
Seafarers_Manual
/
Source
/
RaisePower4.mod
< prev
next >
Wrap
Text File
|
1993-08-15
|
1KB
|
61 lines
MODULE RaisePower4; (* Raise x to y power (y>=0) using funcion procedure *)
(* From the book "Modula-2 A Seafarer's Manual and Shipyard Guide" *)
(* Page 114 adapted "Amiga M2Modula-2" 08 Mar 1988 *)
FROM InOut IMPORT WriteLn,
WriteString,
ReadCard;
FROM RealInOut IMPORT WriteReal,
ReadReal;
VAR
z, (* receives result *)
x : REAL; (* base from keyboard *)
y : CARDINAL; (* exponent from keyboard *)
PROCEDURE Getxy(VAR base : REAL; (* get x & y from keyboard *)
VAR exp : CARDINAL);
BEGIN
WriteLn;
WriteString ("Enter x: ");
ReadReal (base);
WriteLn;
WriteString ("Enter y: ");
ReadCard (exp);
END Getxy;
PROCEDURE Power(t : REAL; (* base *)
e : CARDINAL) : REAL; (* exponent *)
VAR
xy : REAL;
BEGIN
xy := 1.0; (* initialize result *)
WHILE (e # 0) DO
WHILE (NOT ODD(e)) DO
t := t * t;
e := e DIV 2;
END; (* WHILE NOT *)
xy := xy * t;
DEC (e);
END; (* WHILE e *)
RETURN xy; (* return result *)
END Power;
PROCEDURE DisplayAnswer(xtoy : REAL);
BEGIN
WriteLn;
WriteString ("x to y power = ");
WriteReal (xtoy,10,2);
WriteLn;
END DisplayAnswer;
BEGIN (* MODULE RaisePower *)
WriteLn;
WriteString ("Calculating x to y power");
Getxy(x,y);
z := Power(x,y);
DisplayAnswer(z);
END RaisePower4.